package org.lagou;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.lagou.bean.ConsumerComponet;
import org.lagou.filter.SaveInvokeTime;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.SortedMap;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author junbao
 */
public class AnnotationConsumerMain {

    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        ConsumerComponet consumerComponet = context.getBean(ConsumerComponet.class);
        while (true) {
            // dubbo.provider.threadpool=watching
//            for(int i=0; i<100; i++) {
//                Thread.sleep(100);
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        String hello = consumerComponet.sayHello("world!", 0);
//                        System.out.println(hello);
//                    }
//                }).start();

                Executors.newSingleThreadScheduledExecutor()
                        .scheduleAtFixedRate(new Runnable() {
                            @Override
                            public void run() {
                                int random = (int) (Math.random()*100);
                                String hello = consumerComponet.sayHello("world!", random);
                                System.out.println(hello);
                            }
                        }, 1000, 50, TimeUnit.MILLISECONDS);

                Executors.newSingleThreadScheduledExecutor()
                        .scheduleAtFixedRate(new Runnable() {
                            @Override
                            public void run() {
                                System.out.println("===============================================================");
                                SortedMap<Long, Integer> sortedMap = SaveInvokeTime.sortedMap;
                                System.out.println("最近一分钟内TP90:" + SaveInvokeTime.getTpTime(90));
                                System.out.println("最近一分钟内TP99:" + SaveInvokeTime.getTpTime(99));
                                System.out.println("===============================================================");
                            }
                        }, 60, 5, TimeUnit.SECONDS);

                System.in.read();


//                String hello = consumerComponet.sayHello("world!", 0);
//                System.out.println(hello);
            }
        }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "org.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }

}
